#!/bin/bash

# this is a block design of 29.5s on vs 29.5s off. The timing is
# matched to 16 TRs per block. The experiment starts with rest.
# The onset times are derived as 3.688 (TR in s) x ( 8 + N x 16 )

#The below part of the script is optional. It estimates the percent signal change 
# during early and late (post-stimulus) time points of the response. 

# generating run averages
3dMean -overwrite -prefix trial_averages.nii.gz   \
    BOLD.nii.gz'[0..15]'                          \
    BOLD.nii.gz'[16..31]'                         \
    BOLD.nii.gz'[32..47]'                         \
    BOLD.nii.gz'[48..63]'                         \
    BOLD.nii.gz'[64..79]'                         \
    BOLD.nii.gz'[80..95]'                         \
    BOLD.nii.gz'[96..111]'                        \
    BOLD.nii.gz'[112..127]'                       \
    BOLD.nii.gz'[128..143]'                       \
    BOLD.nii.gz'[144..159]'                       \
    BOLD.nii.gz'[160..175]'                       \
    BOLD.nii.gz'[176..191]'


# calculating percent signal change for main response (TRs 13-15) vs baseline (TRs 6-7)
3dCalc -a BOLD.nii.gz'[13]' -b BOLD.nii.gz'[14]' -c BOLD.nii.gz'[15]'   \
       -d BOLD.nii.gz'[6]' -e BOLD.nii.gz'[7]'                      \
     -overwrite                                                     \
     -expr '((a+b+c)/3-(d+e)/2)/((d+e)/2)*100'                        \
     -prefix beta_percent_beta_main_response.nii.gz 

cp beta_percent_beta_VASO.nii.gz cond_1b.nii.gz 


# calculating percent signal change for post-stimulus undershoot (TRs 3-4) vs baseline (TRs 6-7)
3dCalc -a BOLD.nii.gz'[3]' -b BOLD.nii.gz'[4]'   \
       -d BOLD.nii.gz'[6]' -e BOLD.nii.gz'[7]'                      \
     -overwrite                                                     \
     -expr '((a+b)/-(d+e)/2)/((d+e)/2)*100'                        \
     -prefix beta_percent_beta_PSU_response.nii.gz 

cp beta_percent_beta_main_response.nii.gz cond_1b.nii.gz 
cp beta_percent_beta_PSU_response.nii.gz  cond_2b.nii.gz 




